package ModelLayer;

import java.util.ArrayList;
import java.util.Iterator;

public class LeaseItem extends Item {

	private static final long serialVersionUID = 1L;
	private static int lastID = 0;
	private int id;
	private double price;
	private ArrayList<LeaseItemUnit> leaseItemUnits;

	public LeaseItem() {
		super();
		this.id = generateID();
		this.price = 0;
		this.leaseItemUnits = new ArrayList<LeaseItemUnit>();
	}

	public LeaseItem(String name, int location, double price) {
		super(name, location);
		this.id = generateID();
		this.price = price;
		this.leaseItemUnits = new ArrayList<LeaseItemUnit>();
	}

	public void createUnit() throws RealException {
		Status status = Status.Available;
		LeaseItemUnit leaseItemUnit = new LeaseItemUnit(this, status);
		leaseItemUnits.add(leaseItemUnit);
		if (!leaseItemUnits.contains(leaseItemUnit)) {
			throw new RealException(
					"Something went wrong during creation of lease item unit!");
		}
	}

	public LeaseItemUnit getUnit(int serialNo) throws RealException {
		LeaseItemUnit theUnit = null;
		Iterator<LeaseItemUnit> it = leaseItemUnits.iterator();
		boolean found = false;
		while (it.hasNext() && !found) {
			LeaseItemUnit unit = it.next();
			if (unit.getSerialNo() == serialNo) {
				theUnit = unit;
				found = true;
			}
		}
		if (!found)
			throw new RealException("Lease item unit not found!");
		return theUnit;
	}

	public void updateUnitStatus(int serialNo, String passedStatus)
			throws RealException {
		LeaseItemUnit unit = getUnit(serialNo);
		int status = 0;
		try {
			status = Integer.parseInt(passedStatus);
		} catch (NumberFormatException NFe) {
			throw new RealException("Status value was not a number. " + NFe);
		}
		if (status >= 1 && status <= 5) {
			switch (status) {
			// 1 for available
			case 1:
				unit.setStatus(Status.Available);
				break;
			// 2 for borrowed
			case 2:
				unit.setStatus(Status.Borrowed);
				break;
			// 3 for BorrowedLease
			case 3:
				unit.setStatus(Status.BorrowedLease);
				break;
			// 4 for ReturnedLease
			case 4:
				unit.setStatus(Status.ReturnedLease);
				break;
			// 5 for ReturnedLeaseExpired
			case 5:
				unit.setStatus(Status.ReturnedLeaseExpired);
			}
		} else {
			throw new RealException(
					"Passed status number is not in range 1 to 5");
		}
	}

	public void deleteUnit(int serialNo) throws RealException {
		LeaseItemUnit unit = getUnit(serialNo);
		if (unit != null) {
			leaseItemUnits.remove(unit);
		}
	}

	public String getUnitInfo(int serialNo) throws RealException {
		LeaseItemUnit unit = getUnit(serialNo);
		return unit.toString();
	}

	public ArrayList<String> listAllUnits() {
		ArrayList<String> unitsInfo = new ArrayList<String>();
		if (leaseItemUnits.size() != 0) {
			for (LeaseItemUnit unit : leaseItemUnits) {
				unitsInfo.add(unit.toString());
			}
		}
		if (unitsInfo.size() != 0) {
			return unitsInfo;
		} else {
			unitsInfo.add("There are none");
			return unitsInfo;
		}
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	public ArrayList<LeaseItemUnit> getLeaseItemUnits() {
		return leaseItemUnits;
	}

	public void setLeaseItemUnits(ArrayList<LeaseItemUnit> leaseItemUnits) {
		this.leaseItemUnits = leaseItemUnits;
	}

	public int generateID() {
		return ++lastID;
	}

	public static int getLastID() {
		return lastID;
	}

	public static void setLastId(int lastid) {
		lastID = lastid;
	}
	
	public ArrayList<String> showLeaseItemInfo() {
		ArrayList<String> info = new ArrayList<String>();
		info.add("Id: " + getId() + ", name: " + getName() + ", price: " + getPrice() +  ", location: " + getLocation() + "\nLease item units: ");
		ArrayList<String> unitsInfo = listAllUnits();
		for(String unitInfo : unitsInfo)
			info.add(unitInfo);
		info.add("\n");
		return info;
	}

	public String getDetails() {
		String details = "";
		details += "\nName: " + getName() + ", Location: " + getLocation() + "\n";
		return details;
	}
	
	public int getFreeUnits()
	{
		int cont=0;
		for(LeaseItemUnit unit: leaseItemUnits)
		{
			if(unit.getStatus().equals(Status.Available))
				cont++;
		}
		return cont;
	}
}
